#include "philosopher.h"

// TODO: define some sem if you need

int chopsticks[PHI_NUM];
void init() {
  // init some sem if you need
  for (int i = 0; i < PHI_NUM; i++) {
    chopsticks[i] = sem_open(1);
  }
}

void philosopher(int id) {
  // implement philosopher, remember to call `eat` and `think`
  while (1) {
    // 约定率先拿编号小的筷子
    int small_id = 0;
    int big_id = 0;
    if (id > (id + 1) % PHI_NUM) {
      small_id = (id + 1) % PHI_NUM;
      big_id = id;
    } else {
      small_id = id;
      big_id = (id + 1) % PHI_NUM;
    }

    think(id);
    P(chopsticks[small_id]);
    P(chopsticks[big_id]);
    eat(id);
    V(chopsticks[small_id]);
    V(chopsticks[big_id]);
  }
}
